I have a string like this "2012.12.04T08:35:00" that represents a time in the "W. Europe Standard Time" timezone.
Now, I want to convert this properly to a c# DateTime object in UTC time.
What is the proper way to do this?
home / developersection / forums / convert time in string to utc time?
I have a string like this "2012.12.04T08:35:00" that represents a time in the "W. Europe Standard Time" timezone.
Now, I want to convert this properly to a c# DateTime object in UTC time.
What is the proper way to do this?
Pravesh Singh
31-Jan-2014Use TimeZoneInfo when converting between specific time zones:
TimeZoneInfo westInfo = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");DateTime westTime = DateTime.Parse("2012.12.04T08:35:00");
DateTime utcTime = TimeZoneInfo.ConvertTimeToUtc(westTime, westInfo);
To address your confusion:
DateTime.Parse as used here makes no assumptions about the timezone of the given value. IT stores it with a DateTimeKind of Unspecified.
TimeZoneInfo.ConvertTimeToUtc as used here expects an Unspecified datetime, reads it as if it is in the explicitly specified time zone, and converts it to UTC.